var express = require('express');
var router = express.Router();
const { jwt, jwtKey } = require('../server_jwt');
let db = require('../db');

router.post('/create', function (req, res, next) {
    if (!req.body.jwt) {
        res.status(500).send({msg: "令牌为空", login_status: false});
    } else {
        jwt.verify(req.body.jwt, jwtKey, (err, decoded) => {
            if (err) {
                // 解析 jwt 出现错误
                res.status(500).send({msg: "登录验证失败，请重新登录", login_status: false});
            } else if (parseInt(+new Date() / 1000) >= decoded.exp) {
                // jwt 登录超时，需要重新登录
                res.status(500).send({msg: "登录超时:", login_status: false});
            } else if (decoded.uid == req.body.uid) {
                if (!req.body.content) {
                    res.status(500).send({msg: "发布内容为空", login_status: false});
                } else {
                    let sql_post = 'insert into dynamic(uid, content, look) values (?, ?, ?)';
                    let data = [decoded.uid, req.body.content, req.body.look];

                    db.query(sql_post, data, (err) => {
                        if (err) {
                            console.log(err.message);
                        }
                        res.status(200).send("");
                    });
                }
            } else {
                res.status(500).send({msg: "用户不一致", login_status: false});
            }
        });
    }
});

router.post('/showAlldynamic', function (req, res, next) {
    if (!req.body.jwt) {
        res.status(500).send({msg: "令牌为空", login_status: false});
    } else {
        jwt.verify(req.body.jwt, jwtKey, (err, decoded) => {
            if (err) {
                // 解析 jwt 出现错误
                res.status(500).send({msg: "登录验证失败，请重新登录", login_status: false});
            } else if (parseInt(+new Date() / 1000) >= decoded.exp) {
                // jwt 登录超时，需要重新登录
                res.status(500).send({msg: "登录超时:", login_status: false});
            } else {
                // let sql_post = 'select dynamic.id as id, users.name as name, dynamic.content as content from dynamic inner join users as u on dynamic.uid=u.id where dynamic.uid=? or dynamic.look=0';
                let sql_post = `SELECT dynamic.id AS id, users.name AS name, dynamic.content AS content 
                    FROM dynamic 
                    INNER JOIN users ON dynamic.uid = users.id 
                    WHERE dynamic.uid=? OR dynamic.look=0`;
                let data = [decoded.uid];

                db.query(sql_post, data, (err, result) => {
                    if (err) {
                        console.log(err.message);
                    }
                    console.log("result", result);
                    res.status(200).json(result);
                });
            }
        });
    }
});

router.post('/show_one_dynamic', function (req, res, next) {
    if (!req.body.jwt) {
        res.status(500).send({msg: "令牌为空", login_status: false});
    } else {
        jwt.verify(req.body.jwt, jwtKey, (err, decoded) => {
            if (err) {
                // 解析 jwt 出现错误
                res.status(500).send({msg: "登录验证失败，请重新登录", login_status: false});
            } else if (parseInt(+new Date() / 1000) >= decoded.exp) {
                // jwt 登录超时，需要重新登录
                res.status(500).send({msg: "登录超时:", login_status: false});
            } else {
                let sql_post = `SELECT users.name AS name, dynamic.content AS content 
                    FROM dynamic 
                    INNER JOIN users ON dynamic.uid = users.id 
                    WHERE dynamic.id=?`;
                let data = [req.body.id];

                db.query(sql_post, data, (err, result) => {
                    if (err) {
                        console.log(err.message);
                    }
                    console.log("result", result);
                    res.status(200).json(result[0]);
                });
            }
        });
    }
});

module.exports = router;
